/*******************************************************************************
 * Copyright 2007 Vidar Svansson
 *     
 * This file is part of JAdapter.
 * 
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/


package org.jadapter.tests;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.jadapter.JAdapter;
import org.jadapter.Query;
import org.jadapter.registry.Components;
import org.jadapter.registry.SimpleComponentRegistry;
import org.jadapter.tests.examples.CommaSeparated;
import org.jadapter.tests.examples.CommaSeparatedFactory;
import org.jadapter.tests.examples.CommaSeparatedList;
import org.jadapter.tests.examples.CommaSeparatedMap;

@SuppressWarnings("unchecked")
public abstract class BaseTestCase extends TestCase {
    
    protected Map<String, String> map;
    protected JAdapter<CommaSeparated,Map> mapAdapter;
    
    protected List<?> list;
    protected JAdapter<CommaSeparated,List> listAdapter;
    
    protected JAdapter<CommaSeparated,?> commaSeparatedFactory;
    
    protected CommaSeparated commas; 
    

    protected Query<String> query;
    protected SimpleComponentRegistry registry;    
    
    
    @Override
    public void setUp() {       
        commaSeparatedFactory = new JAdapter<CommaSeparated,Object>(
                CommaSeparatedFactory.class,
                CommaSeparated.class,
                Object.class);      
        
        map = new HashMap<String,String>();
        map.put("Foo", "foo");
        map.put("Bar", "bar");
        
        mapAdapter = new JAdapter<CommaSeparated,Map> ();
        mapAdapter.setAdapter(CommaSeparatedMap.class);
        mapAdapter.setProvides(CommaSeparated.class);
        mapAdapter.setFor(Map.class);       
        
        
        list = Arrays.asList("Foo", "Bar");
        
        listAdapter = new JAdapter<CommaSeparated,List> (
                CommaSeparatedList.class,
                CommaSeparated.class,
                List.class);
        
        query = new Query<String>("a", "b", "c");
        registry = new SimpleComponentRegistry();
    }

    @Override
    protected void tearDown() throws Exception {
        Components.destroy();
    }   
    
    public <T> void assertQueryEquals(Query<T> mengi, List<T> args){
        int i = 0;
        for(T s : mengi){
            T x = args.get(i++);
            if(!s.equals(x)){
                fail(s + " != " + x);
            }
        }
    }    

    
    public <T> void assertQueryEquals(Query<T> mengi, T ... args){
        assertQueryEquals(mengi, Arrays.asList(args));
    }    
    

}
